<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 请求的函数封装
    const request = {
      get: (url, params) => {
        const str = Object.keys(params).map(item => { // ["pageSize", "pageNum", "seachValue"]
          return item + "=" + params[item]  // ["pageSize=3","pageNum=1", "seachValue=''"]
        }).join("&") // "pageSize=3&pageNum=1&seachValue=''"

        return fetch("http://127.0.0.1:3000/api" + url + "?" + str, {
          headers: {
            // 前端推荐在请求头里面传递token
            token: localStorage.getItem("token")
          }
        }).then(response => response.json())
      },
      post: (url, params) => {
        return fetch("http://127.0.0.1:3000/api" + url, {
          method: "POST",
          body: JSON.stringify(params),
          headers: {"content-type": "application/json"}
        }).then(response => response.json())
      }
    }

    // 列表数据请求
    function getData() {
      request.get("/index/list", {
        pageSize: 3,
        pageNum: 1,
        seachValue: ""
      }).then(res => {
        console.log(res);
      })
    }
    getData();

    // request("/abc/xy", {name: "zhangsan"}).then(res => {
    //   console.log(res);
    // })

    request.post("/index/del", {removeId: "68f093e7a9c947cf6ccebea4"}).then(res => {
      console.log(res);
    })
    
  </script>
</body>
</html>